import uploadMedia from '@/components/upload-media'
import engineerList from '@/components/engineer-list'
import {addObj, putObj} from '../../../api/finance-manage-charge'
import {mapState} from 'vuex'
import BaseLoad from '@/assets/vue-mixin/BaseLoad'

export default {
    mixins: [BaseLoad],
    components: {uploadMedia, engineerList},
    computed: {
        ...mapState({
            charge_project_type: state => {
                let data = state.dict.find(item => item.type === "charge_project_type") || {}
                let list = data.children || []
                return data.children || []
            },
            rounding_type: state => {
                let data = state.dict.find(item => item.type === "rounding_type") || {}
                let list = data.children || []
                return data.children || []
            },
            keep_type: state => {
                let data = state.dict.find(item => item.type === "keep_type") || {}
                let list = data.children || []
                return data.children || []
            },
        })
    },
    data() {
        return {
            fileSize: 50,
            formData: {},
            input: '',
            value: '',
            area: [],
            form: {
                chargeProjectType: '',
                chargeProjectName: '',
                financeCode: '',
                receivablesDate: '',
                roundingType: '',
                keepType: ''
            },
            dialogVisible: false,
            isEdit: false,
            rules: {
                chargeProjectType: [{required: true, message: '请选择收费项目类型', trigger: 'blur'}],
                chargeProjectName: [{required: true, message: '请输入收费项目名称', trigger: 'blur'}],
                financeCode: [{required: true, message: '请输入对应财务系统费用编码', trigger: 'change'}],
                receivablesDate: [{required: true, message: '请输入应收日期设置', trigger: 'change'}],
                roundingType: [{required: true, message: '请选择取整方式', trigger: 'blur'}],
                keepType: [{required: true, message: '请选择金额保留位', trigger: 'blur'}],

            }
        }
    },
    methods: {
        show(item) {
            this.dialogVisible = true
            setTimeout(() => {
                this.isEdit = false
                this.$refs.editForm.resetFields()
                delete this.form.propertyId
                if (item) {
                    this.isEdit = true
                    this.form = item
                    Object.keys(this.form).forEach(key => {
                        this.form[key] = item[key]
                    })
                    this.form.propertyId = item.propertyId
                }
            }, 200)
        },
        commit() {
            this.$refs.editForm.validate(valid => {
                if (valid) {
                    if (this.isEdit) {
                        putObj(this.form).then(resp => {
                            if (resp.data.data) {
                                this.suc('提交成功')
                                this.dialogVisible = false
                                this.$emit('resetList')
                            } else {
                                this.err('提交失败')
                            }
                        })
                    } else {
                        addObj(this.form).then(resp => {
                            if (resp.data.data) {
                                this.suc('添加成功')
                                this.dialogVisible = false
                                this.$emit('resetList')
                            } else {
                                this.err('添加失败')
                            }
                        })
                    }
                }
            })
        },
        beforeUpload() {
            this.loadAction('请稍等...')
        }
    }
}
